﻿using LiveCharts.Configurations;
using LiveCharts;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Threading;
using System.Windows.Threading;
using MyWPF.StaticData;
using System.Windows.Input;
using Prism.Commands;
using MyWPF.Views.Subpage.MainPage_Son;
using System.Windows.Controls;

namespace MyWPF.ViewModel.Subpage
{
    public class VM_MainPage : BaseViewModel
    {
        public VM_MainPage()
        {
            Son1_Checked = true;
            _toMainPage_Son1 = new DelegateCommand<object>(_ToMainPage_Son1);
            _toMainPage_Son2 = new DelegateCommand<object>(_ToMainPage_Son2);
            _toMainPage_Son3 = new DelegateCommand<object>(_ToMainPage_Son3);
            RefreshChart();
            RefreshCard1Chart();
            LoadTime();

            StaticModels.mainPage_Son1 = new MainPage_Son1();
            Content = StaticModels.mainPage_Son1;
        }
        #region 基础：页面切换
        private UserControl _content;
        public UserControl Content
        {
            get { return _content; }
            set { SetProperty(ref _content, value); }
        }
        /// <summary>
        /// 切换到子页1
        /// </summary>
        private ICommand _toMainPage_Son1;
        public ICommand ToMainPage_Son1
        {
            get { return _toMainPage_Son1; }
            set
            {
                _toMainPage_Son1 = value;
                OnPropertyChanged("ToMainPage_Son1");
            }
        }
        private void _ToMainPage_Son1(object obj)
        {
            ExecutePage(MainPages.MainPage_Son1);
        }
        /// <summary>
        /// 切换到子页2
        /// </summary>
        private ICommand _toMainPage_Son2;
        public ICommand ToMainPage_Son2
        {
            get { return _toMainPage_Son2; }
            set
            {
                _toMainPage_Son2 = value;
                OnPropertyChanged("ToMainPage_Son2");
            }
        }
        private void _ToMainPage_Son2(object obj)
        {
            ExecutePage(MainPages.MainPage_Son2);
        }
        /// <summary>
        /// 切换到子页3
        /// </summary>
        private ICommand _toMainPage_Son3;
        public ICommand ToMainPage_Son3
        {
            get { return _toMainPage_Son3; }
            set
            {
                _toMainPage_Son3 = value;
                OnPropertyChanged("ToMainPage_Son3");
            }
        }
        private void _ToMainPage_Son3(object obj)
        {
            ExecutePage(MainPages.MainPage_Son3);
        }
        public enum MainPages
        {
            MainPage_Son1, MainPage_Son2, MainPage_Son3
        }
        public void ExecutePage(MainPages page)
        {
            switch (page)
            {
                case MainPages.MainPage_Son1:
                    StaticModels.mainPage_Son1 = new MainPage_Son1();
                    Content = StaticModels.mainPage_Son1;
                    break;
                case MainPages.MainPage_Son2:
                    StaticModels.mainPage_Son2 = new MainPage_Son2();
                    Content = StaticModels.mainPage_Son2;
                    break;
                case MainPages.MainPage_Son3:
                    StaticModels.mainPage_Son3 = new MainPage_Son3();
                    Content = StaticModels.mainPage_Son3;
                    break;

            }
        }
        private void OnPropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region 属性绑定：按钮
        private bool _son1_Checked;
        public bool Son1_Checked
        {
            
            get { return _son1_Checked; }
            set
            {
                if (value)
                {
                    Son2_Checked = false;
                    Son3_Checked = false;
                }

                SetProperty(ref _son1_Checked, value);
            }
        }
        private bool _son2_Checked;
        public bool Son2_Checked
        {
            get { return _son2_Checked; }
            set
            {
                if (value)
                {
                    Son1_Checked = false;
                    Son3_Checked = false;
                }

                SetProperty(ref _son2_Checked, value);
            }
        }
        private bool _son3_Checked;
        public bool Son3_Checked
        {
            get { return _son3_Checked; }
            set
            {
                if (value)
                {
                    Son1_Checked = false;
                    Son2_Checked = false;
                }

                SetProperty(ref _son3_Checked, value);
            }
        }
        #endregion

        #region 属性绑定/事件：时间
        private String _date;
        public String Date
        {
            get { return _date; }
            set { SetProperty(ref _date, value); }
        }
        private String _week;
        public String Week
        {
            get { return _week; }
            set { SetProperty(ref _week, value); }
        }
        private String _time;
        public String Time
        {
            get { return _time; }
            set { SetProperty(ref _time, value); }
        }
        private void LoadTime()
        {
            Task.Run(async () =>
            {
                while(true)
                {
                    await Task.Delay(1000);
                    Date = DateTime.Now.ToString("yyyy/MM/dd");
                    Week = DateTime.Now.DayOfWeek.ToString();
                    Time = DateTime.Now.ToString("HH:mm:ss");
                }
                
            });
            

            
        }
        #endregion

        #region 属性绑定/事件：主livechart曲线绑定属性
        private ChartValues<LvcModel1> _chartDatas1;
        public ChartValues<LvcModel1> ChartDatas1
        {
            get { return _chartDatas1; }
            set { SetProperty(ref _chartDatas1, value); }
        }
        private ChartValues<LvcModel2> _chartDatas2;
        public ChartValues<LvcModel2> ChartDatas2
        {
            get { return _chartDatas2; }
            set { SetProperty(ref _chartDatas2, value); }
        }

        private double _AxisMin2;
        public double AxisMin2
        {
            get { return _AxisMin2; }
            set { SetProperty(ref _AxisMin2, value); }
        }
        private double _axisMax2;
        public double AxisMax2
        {
            get { return _axisMax2; }
            set { SetProperty(ref _axisMax2, value); }
        }
        private double _axisStep2;
        public double AxisStep2
        {
            get { return _axisStep2; }
            set { SetProperty(ref _axisStep2, value); }
        }
        private Func<double, string> _xLabelFormatter;
        public Func<double, string> XLabelFormatter
        {
            get { return _xLabelFormatter; }
            set { SetProperty(ref _xLabelFormatter, value); }
        }

        private object PointFill1(LvcModel1 model)
        {
            if (model.Value < 0.3)
            {
                return Brushes.Green;
            }
            else if (model.Value > 0.8)
            {
                return Brushes.Red;
            }
            else
            {
                return Brushes.Transparent;
            }
        }
        private object PointFill2(LvcModel2 model)
        {
            if (model.Value < 50)
            {
                return Brushes.Green;
            }
            else if (model.Value > 130)
            {
                return Brushes.Red;
            }
            else
            {
                return Brushes.Transparent;
            }
        }

        private string GetFormatter(double value)
        {
            DateTime dt = new DateTime((long)value);
            return dt.ToString("mm:ss");
        }

        private void RefreshChart()
        {
            XLabelFormatter = new Func<double, string>(GetFormatter);

            ChartDatas1 = new ChartValues<LvcModel1>();
            ChartDatas2 = new ChartValues<LvcModel2>();
            AxisStep2 = TimeSpan.FromSeconds(5).Ticks;
            Random random = new Random();
            var mapper1 = Mappers.Xy<LvcModel1>()
                .X(model => model.DateTime.Ticks)
                .Y(model => model.Value)
                .Stroke(PointFill1)
                .Fill(PointFill1);
            Charting.For<LvcModel1>(mapper1);
            var mapper2 = Mappers.Xy<LvcModel2>()
                .X(model => model.DateTime.Ticks)
                .Y(model => model.Value)
                .Stroke(PointFill2)
                .Fill(PointFill2);
            Charting.For<LvcModel2>(mapper2);

            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(2000);
                    var now = DateTime.Now;
                    AxisMax2 = now.Ticks;
                    AxisMin2 = now.Ticks - TimeSpan.FromSeconds(60).Ticks;

                    ChartDatas1.Add(new LvcModel1
                    {
                        Value = random.NextDouble(),
                        DateTime = now,
                    });
                    if (ChartDatas1.Count > 23)
                    {
                        ChartDatas1.RemoveAt(0);
                    }


                }
            }); Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(2000);
                    var now = DateTime.Now;
                    AxisMax2 = now.Ticks;
                    AxisMin2 = now.Ticks - TimeSpan.FromSeconds(60).Ticks;

                    ChartDatas2.Add(new LvcModel2
                    {
                        Value = random.Next(30, 150),
                        DateTime = now,
                    });
                    if (ChartDatas2.Count > 32)
                    {
                        ChartDatas2.RemoveAt(0);
                    }


                }
            });
            var now1 = DateTime.Now;
            AxisMax2 = now1.Ticks;
            AxisMin2 = now1.Ticks - TimeSpan.FromSeconds(60).Ticks;
        }
        #endregion

        #region 属性绑定/事件：Card livechart曲线绑定属性
        private double _card1Data;
        public double Card1Data
        {
            get { return _card1Data; }
            set 
            {
                
                SetProperty(ref _card1Data, value); 
            }
        }

        private ChartValues<Card1Model> _card1Datas;
        public ChartValues<Card1Model> Card1Datas
        {
            get { return _card1Datas; }
            set { SetProperty(ref _card1Datas, value); }
        }
        

        private double _card1Min;
        public double Card1Min
        {
            get { return _card1Min; }
            set { SetProperty(ref _card1Min, value); }
        }
        private double _card1Max;
        public double Card1Max
        {
            get { return _card1Max; }
            set { SetProperty(ref _card1Max, value); }
        }
        private double _card1Step;
        public double Card1Step
        {
            get { return _card1Step; }
            set { SetProperty(ref _card1Step, value); }
        }

        private object Card1PointFill(Card1Model model)
        {
            return Brushes.Transparent;
        }

        private void RefreshCard1Chart()
        {
            XLabelFormatter = new Func<double, string>(GetFormatter);

            Card1Datas = new ChartValues<Card1Model>();
            Card1Step = TimeSpan.FromSeconds(5).Ticks;
            Random random = new Random();
            var mapper1 = Mappers.Xy<Card1Model>()
                .X(model => model.DateTime.Ticks)
                .Y(model => model.Value)
                .Stroke(Card1PointFill)
                .Fill(Card1PointFill);
            Charting.For<Card1Model>(mapper1);
            Task.Run(async () =>
            {
                double data = 0.5;
                while (true)
                {
                    await Task.Delay(1000);
                    var now = DateTime.Now;
                    Card1Max = now.Ticks;
                    Card1Min = now.Ticks - TimeSpan.FromSeconds(20).Ticks;
                    double NextData = random.NextDouble();
                    data = (NextData + data) / 2;
                    if (data > 1)
                    {
                        data = 1;
                    }
                    
                    Card1Data = Convert.ToDouble((data * 100).ToString("f1"));

                    Card1Datas.Add(new Card1Model
                    {
                        Value = data,
                        DateTime = now,
                    });
                    if (Card1Datas.Count > 23)
                    {
                        Card1Datas.RemoveAt(0);
                    }


                }
            }); 
            var now1 = DateTime.Now;
            Card1Max = now1.Ticks;
            Card1Min = now1.Ticks - TimeSpan.FromSeconds(40).Ticks;
        }
        #endregion
    }
    public class LvcModel1
    {
        public DateTime DateTime { get; set; }
        public double Value { get; set; }
    }
    public class LvcModel2
    {
        public DateTime DateTime { get; set; }
        public double Value { get; set; }
    }
    public class Card1Model
    {
        public DateTime DateTime { get; set; }
        public double Value { get; set; }
    }
}
